package com.ctsi.ssdc.service.impl;


import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import com.ctsi.ssdc.domain.LitemallIssue;
import com.ctsi.ssdc.domain.LitemallIssueExample;
import com.ctsi.ssdc.service.LitemallIssueService;
import com.ctsi.ssdc.repository.LitemallIssueRepository;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.Objects;
import com.ctsi.ssdc.service.impl.StrengthenBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctsi.ssdc.model.PageResult;
import org.springframework.data.domain.Pageable;
import com.github.pagehelper.PageHelper;
/**
 * Service Implementation for managing LitemallIssue.
 *
 * @author ctsi-biyi-generator
 *
 */
@Service
public class LitemallIssueServiceImpl 
	extends StrengthenBaseServiceImpl<LitemallIssueRepository, LitemallIssue, Integer, LitemallIssueExample> 
	implements LitemallIssueService {

    @Autowired
    LitemallIssueRepository litemallIssueRepository;


    /**
    * GET  /litemallIssues : get the litemallIssues firstStringBaseColumn.
    */
    @Override
    public PageResult<LitemallIssue> findFirstStringColumn(String question,Pageable pageable){
        String str = question;
        if (Objects.nonNull(pageable)) {
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        }
        LitemallIssueExample litemallIssueExample = new LitemallIssueExample();
        String orderBy = getPageOrderBy(pageable);
        if(StringUtils.isNotEmpty(orderBy)) {
            litemallIssueExample.setOrderByClause(orderBy);
        }
        if (StringUtils.isEmpty(str)){
            litemallIssueExample.createCriteria();
        } else{
            litemallIssueExample.createCriteria().andQuestionLike("%" + str +"%");
        }
        List<LitemallIssue>  data = litemallIssueRepository.selectByExample(litemallIssueExample);

        long count = 0L;
        if (CollectionUtils.isNotEmpty(data)){
            count = litemallIssueRepository.countByExample(litemallIssueExample);
        }
        return new PageResult<LitemallIssue>(data,count,count);
    }

    @Override
    public List<LitemallIssue> querySelective(String question, Integer page, Integer limit, String sort, String order) {
        LitemallIssueExample example = new LitemallIssueExample();
        LitemallIssueExample.Criteria criteria = example.createCriteria();

        if (!org.springframework.util.StringUtils.isEmpty(question)) {
            criteria.andQuestionLike("%" + question + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!org.springframework.util.StringUtils.isEmpty(sort) && !org.springframework.util.StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return litemallIssueRepository.selectByExample(example);
    }

    private String getPageOrderBy(Pageable page) {
        if(page!= null && page.getSort() != null) {
            StringBuilder sb = new StringBuilder();
            page.getSort().forEach(sort -> sb.append(sort.getProperty())
            .append(" ").append(sort.getDirection()).append(","));
            if(sb.length() > 1) {
                return (sb.substring(0,sb.length()-1));
             }
        }
        return null;
    }
}
